import { getStore, setStore } from "@/utils/store.js";
const state = {
  historyList: getStore("historyList") || [],
};
const mutations = {
  SET_HISTORY_LIST: (state, historyList) => {
    state.historyList = historyList;
    setStore("historyList", historyList);
  },
};
const actions = {
  updateHistoryList({ commit, state }, keyword) {
    if (!state.historyList.includes(keyword)) {
      const list = state.historyList;
      list.push(keyword);
      commit("SET_HISTORY_LIST", list);
    } else {
      const index = state.historyList.findIndex((item) => {
        return item === keyword;
      });
      const list = state.historyList;
      list.splice(index, 1);
      list.unshift(keyword);
      commit("SET_HISTORY_LIST", list);
    }
  },
  clearHistoryList({ commit, state }) {
    if (state.historyList.length > 0) {
      commit("SET_HISTORY_LIST", []);
    }
  },
};

export default { state, mutations, actions };
